﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCM_maps_website.Models;

namespace HCM_maps_website.Process.DAL
{
    public class PlaceDAO
    {
        MapDataDataContext context = new MapDataDataContext();

        public IList<PlaceModels> GetExactAddress(string number, string street)
        {
            IList<PlaceModels> result = new List<PlaceModels>();

            var query = context.sp_Address_FindExactAddress(number, street);

            AssignPlace(result, query);

            return result;
        }

        private static void AssignPlace(IList<PlaceModels> result, System.Data.Linq.ISingleResult<sp_Address_FindAddressResult> query)
        {
            foreach (var record in query.ToList())
            {
                PlaceModels p = new PlaceModels();
                p.HomeNumber = record.Number;
                p.Area = record.Area;
                p.coordinate = new Coordinate(record.xLocation, record.yLocation);
                result.Add(p);
            }
        }

        public IList<PlaceModels> GetNearAddress(string number, string street)
        {
            IList<PlaceModels> result = new List<PlaceModels>();

            var query = context.sp_Address_FindNearAddress(number, street);

            AssignPlace(result, query);

            return result;
        }

        public PlaceModels GetNearCoordinate(Coordinate start)
        {
            PlaceModels place = new PlaceModels();

            var selected = context.FindNearCoordinate(start.Longitude, start.Latitude);

            foreach (var record in selected.ToList())
            {
                place.HomeNumber = record.Number;
                place.Area = record.Area;
                place.coordinate = new Coordinate(record.xLocation, record.yLocation);
            }

            return place;
        }
    }
}